﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;

namespace LdExpress
{
    public class PaymentRequestDetail
    {
        public long ID = -1;
        public string FileNumber = "";
        public string MBL = "";
        public string HBL = "";
        public string Note = "";
        public int Amount = 0;
        public long PaymentRequestID = 0;
        public double TotalCost = 0;
        public double TotalRequest = 0;

        public PaymentRequestDetail()
        {
        }

        public PaymentRequestDetail(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_paymentrequest_detail";

            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = long.Parse(DBModule.GetNewID(typeof(Bill), "tbl_bill" , "ID", "ID", cn, trans));
                strSQL = "Insert into " + strTableName + " (" +
                                "FileNumber" +
                                ",MBL" +
                                ",HBL" +
                                ",Note" +
                                ",Amount" +
                                ",TotalCost" +
                                ",TotalRequest" +
                                ",PaymentRequestID" +
                             ") " +
                         " values(" +
                             "'" + DBModule.RefineString(FileNumber) + "'" +
                             "," + "'" + DBModule.RefineString(MBL) + "'" +
                             "," + "'" + DBModule.RefineString(HBL) + "'" +
                             "," + "'" + DBModule.RefineString(Note) + "'" +
                             "," + Amount.ToString() +
                             "," + TotalCost.ToString() +
                             "," + TotalRequest.ToString() +
                             "," + PaymentRequestID.ToString() +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("paymentrequest", "add", cn, trans);               
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                        "FileNumber =  '" + DBModule.RefineString(FileNumber) + "'," +
                        "MBL = '" + DBModule.RefineString(MBL) + "'," +
                        "HBL = '" + DBModule.RefineString(HBL) + "'," +
                        "Note = '" + DBModule.RefineString(Note) + "'," +
                        "Amount = " + Amount + "," +
                        "TotalCost = " + TotalCost + "," +
                        "TotalRequest = " + TotalRequest.ToString() + "," +
                        "PaymentRequestID = " + PaymentRequestID.ToString() + 
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("paymentrequest", "edit", cn, trans);                
            }
            //UpdateReceiveAndPaymentReport(this, cn, trans);
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {

            string strSQL = "DELETE FROM tbl_paymentrequest_detail WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("paymentrequest", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_paymentrequest_detail WHERE ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("FileNumber")) FileNumber = dr["FileNumber"].ToString();
                if (!dr.IsNull("MBL")) MBL = dr["MBL"].ToString();
                if (!dr.IsNull("HBL")) HBL = dr["HBL"].ToString();
                if (!dr.IsNull("Note")) Note = dr["Note"].ToString();
                if (!dr.IsNull("Amount")) Amount = int.Parse(dr["Amount"].ToString());
                if (!dr.IsNull("TotalCost")) TotalCost = double.Parse(dr["TotalCost"].ToString());
                if (!dr.IsNull("TotalRequest")) TotalRequest = double.Parse(dr["TotalRequest"].ToString());
                if (!dr.IsNull("PaymentRequestID")) PaymentRequestID = long.Parse(dr["PaymentRequestID"].ToString());

            }
        }

        public static void GetList(string Condittion, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "Select * from tbl_paymentrequest_detail where 1=1 ";
            if ((Condittion != "") && (Condittion != null))
            {
                strSQL += Condittion;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            //GV.Response.Write("sql=" + strSQL);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_paymentrequest_detail where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}

